package algo_annotations.ihm.couche;

import algo_annotations.data.annotations.Anno;
import commons.drawing.I_Drawable;
import commons.ihm.ParametresAffichageCarto;
import commons.utils.UtilsGeom;
import commons.utils.primitive_geom.Point_d;
import commons.utils.primitive_geom.Rect_d;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.Rectangle2D;

// Drawable correspondant au bounding box d'une annotation
public class DrawableAnnoBB implements I_Drawable {

	private Anno anno;

	public DrawableAnnoBB(Anno anno) {
		this.anno = anno;
	}


	public void draw(Graphics2D g, ParametresAffichageCarto pac) {
		if (!anno.site.hasAnno()) return; // site sans anno

		g.setStroke(pac.getBasicStroke1());

		// meilleure position de l'annotation, sans interaction possible avec autres annotations
		if (anno.site.anno_almostbestPlacement_noIntereact != null) {
			g.setColor(Color.orange);
			g.draw(getShape(anno.site.anno_almostbestPlacement_noIntereact, pac));
		}

		// meilleure position de l'annotation, independament des autres annotations
		if (anno.site.anno_bestPlacement != null) {
			g.setColor(Color.green);
			g.draw(getShape(anno.site.anno_bestPlacement, pac));
		}

		// les rect des sous-mots si anno multigne
		if (anno.bMultiline) {
			g.setColor(Color.lightGray);
			g.draw(getShape(anno.rect_ligne1_protection_superpositionAnno, pac));
			g.draw(getShape(anno.rect_ligne2_protection_superpositionAnno, pac));

			g.setColor(Color.gray);
			g.draw(getShape(anno.rect_ligne1_protection_superpositionLigneSite, pac));
			g.draw(getShape(anno.rect_ligne2_protection_superpositionLigneSite, pac));

			g.setColor(anno.getColor());
			g.draw(getShape(anno.rect_ligne1, pac));
			g.draw(getShape(anno.rect_ligne2, pac));

		} else {
			g.setColor(anno.getColor());
			g.draw(getShape(anno, pac));

			g.setColor(Color.lightGray);
			g.draw(getShape(anno.rect_protection_superpositionAnno, pac));

			g.setColor(Color.gray);
			g.draw(getShape(anno.rect_protection_superpositionLigneSite, pac));
		}
	}

	private Shape getShape(Rect_d rect, ParametresAffichageCarto pac) {
		Rectangle2D.Float shRect = new Rectangle2D.Float();
		shRect.x = pac.x2X(rect.getXMin());
		shRect.y = pac.y2Y(rect.getYMax());
		shRect.width = pac.lg2LG(rect.getWidth());
		shRect.height = pac.lg2LG(rect.getHeight());
		return shRect;
	}

	public double distTo(Point_d pt) {
		return UtilsGeom.getDistRectPoint(anno, pt.x, pt.y);
	}

	public String getInfoBulle() {
		return "Bounding box de l'anno " + anno.getInfoBulle();
	}

	public String getInfoDetaillee() {
		return anno.getInfoDetaillee();
	}


}
